<template>
  <div class="container">
    <div class="content-wrapper">
      <div class="search-bar">
        <div></div>
        <div @click="openFilterDialog" :style="{color: hasFilter ? '#409eff' : '#303133' }">筛选</div>
      </div>
      <div class="box-card-wrapper" ref="scrollcontainer" v-infinite-scroll="loadData" infinite-scroll-disabled="disabled" infinite-scroll-immediate="false">
        <el-card class="box-card" v-for="item in tableData" :key="item.id">
          <div @click="showPiecePage(item)" style="padding: 20px;">
            <div class="rows">
              <span class="title">{{item.planNumber}}<span v-if="item.planName">：{{item.planName}}</span></span>
            </div>
            <div class="rows" v-if="item.planBelongName">
              <span><span class="item-name">所属模块：</span>{{item.planBelongName}}</span>
            </div>
            <div class="rows">
              <span><span class="item-name">完工数量：</span>{{item.planOrderCompleted}}</span>
              <span><span class="item-name">负责人：</span>{{item.planPrincipalName}}</span>
            </div>
            <div class="rows">
              <span><span class="item-name">计划订单总量：</span>{{item.planOrderVolume}}</span>
              <span><span class="item-name">计划状态：</span>{{planStatus[Number(item.planStatus)-1]}}</span>
            </div>
            <div class="rows">
              <span><span class="item-name">计划完工日期：</span>{{item.planFinishTime}}</span>
            </div>
          </div>
        </el-card>
        <p v-if="loading" class="tips">加载中...</p>
        <p v-if="noMore" class="tips">没有更多了</p>
      </div>
    </div>
    <FilterPage title="筛选" page="productionPlan" @filterData="filterData" ref="filterpage" bottomHeight='46' />
    <Tab currentTab="productionPlanningManagementList"/>
  </div>
</template>

<script>
import dingMixin from  '@/mixins/ding'
import formUtil from "@/utils/formUtil";
import Tab from '../component/Tab'
import FilterPage from '../component/FilterPage'
export default {
  name: '',
  data () {
    return {
      currentScrollTop: 0,
      tableData: [],
      page: 1,
      totalPage: 1,
      pageSize: 10,
      isFirstLoading: true,
      loading: false,
      postForm: {
        planNumber: '',
        startTime: '',
        endTime: '',
        planName: '',
        searchType: '',
      },
      planStatus: ['执行', '完成', '终止'],
    };
  },
  components: {
    Tab, FilterPage
  },
  mixins: [dingMixin],
  computed: {
    disabled () {
      return this.isFirstLoading || this.loading || this.noMore
    },
    noMore () {
      return this.page >= this.totalPage;
    },
    hasFilter() {
      return this.postForm.planNumber !== '' || this.postForm.startTime !== '' || this.postForm.endTime !== '' || this.postForm.planName !== '' || this.postForm.searchType !== ''
    }
  },
  beforeRouteLeave(to,from,next) {
    this.currentScrollTop = this.$refs.scrollcontainer.scrollTop;
    this.mobileStopRefresh()
    next()
  },
  activated() {
    this.mobileGetDevice(({model, brand, version}) => {
      if(brand === 'iPhone') {
        this.mobildPullToRefresh(() => {
          this.getPlanList(true)
        });
      }
    })
    if(window.NEEDREFRESH2) {
      this.getPlanList(true);
      window.NEEDREFRESH2 = false;
    } else {
      this.$refs.scrollcontainer.scrollTop = this.currentScrollTop;
    }
  },
  mounted () {
    this.getPlanList(true)
  },
  methods: {
    loadData() {
      this.loading = true;
      this.page ++;
      this.getPlanList();
    },
    async getPlanList(init) {
      if(init) {
        this.page = 1;
        this.tableData = [];
        this.isFirstLoading = true;
      }
      let {list, totalPage} = await this.$httpN.postB('pc/plan/list',{
        page: this.page,
        limit: this.pageSize,
        planNumber: this.postForm.planNumber,
        planName: this.postForm.planName,
        planFinishTimeStart: this.postForm.startTime,
        planFinishTimeEnd: this.postForm.endTime,
        searchType: this.postForm.searchType,
      });
      this.loading = false;
      this.tableData.push(...list)
      if(init) {
        this.totalPage = totalPage;
        this.$nextTick(() => {
          this.isFirstLoading = false;
        })
      }
    },
    openFilterDialog() {
      this.$refs.filterpage.init();
    },
    async filterData(param) {
      this.postForm = formUtil.deepCopy(param);
      await this.getPlanList(true);
    },
    showPiecePage(item) {
      this.$router.push({ name: 'mpiecework-piece', query: {planNumber: item.planNumber, planBelongId: item.planBelongId, planBelongName: item.planBelongName}})
    }
  }
}
</script>

<style lang='scss' scoped>
.container {
  height: calc(100vh - 46px);
  background-color: #F8F8F8;
  .content-wrapper {
    height: 100%;
    padding: 0 18px 10px;
    .search-bar {
      display: flex;
      justify-content: space-between;
      height: 50px;
      line-height: 50px;
    }
    .box-card-wrapper {
      height: calc(100% - 50px);
      overflow-y: auto;
      .box-card {
        margin-bottom: 10px;
        line-height: 16px;
        &:last-child {
          margin-bottom: 0;
        }
        /deep/ .el-card__body {
          padding: 0;
        }
        .rows {
          margin-bottom: 12px;
          font-size: 14px;
          color: #303334;
          display: flex;
          justify-content: space-between;
          &:last-child {
            margin-bottom: 0;
          }
          .item-name {
            color: #AEAEB0;
          }
        }
        .title {
          font-weight: 700;
          font-size: 16px;
        }
      }
      .tips {
        text-align: center;
        color: #909399;
      }
    }
  }
}
</style>